//
//  SDCityPickerViewController.m
//  miaohu
//
//  Created by slowdony on 2017/8/28.
//  Copyright © 2017年 SlowDony. All rights reserved.
//


#import "SDCityPickerViewController.h"

#import "SDCityPickerTableView.h"
#import "SDCityModel.h"

#import "SDCityInitial.h"

#import "TLCityPickerSearchResultController.h"

@interface SDCityPickerViewController ()
<SDBaseTableViewDelegate,TLSearchResultControllerDelegate>
@property (nonatomic,strong)SDCityPickerTableView *cityTableView;

@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) TLCityPickerSearchResultController *searchResultVC;

@end

@implementation SDCityPickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"选择定位城市";
    [self setupTableView];
    [self addNotification];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(SDCityPickerTableView *)cityTableView{
    if (!_cityTableView){
        //
        _cityTableView = [[SDCityPickerTableView alloc] initWithFrame:CGRectMake(0,60, VIEW_WIDTH, VIEW_HEIGHT) style:UITableViewStylePlain];
        _cityTableView.sd_delegate =self;
        
    }
    return _cityTableView;
}

-(void)setupTableView{
    
    UIView * seachView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 60)];
    seachView.backgroundColor = [UIColor backGroudColorForView];
    [self.view addSubview:seachView];
    [seachView addSubview:self.searchController.searchBar];
    
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 50)];
    UILabel * againLocationLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, VIEW_WIDTH-20, 50)];
    againLocationLabel.text = @"重新定位";
    againLocationLabel.userInteractionEnabled = YES;
    [headerView addSubview:againLocationLabel];
    
    UITapGestureRecognizer * againLocationLabelTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(againLocationLabelClick)];
    [againLocationLabel addGestureRecognizer:againLocationLabelTap];
    
    [self.view addSubview:self.cityTableView];
    self.cityTableView.dataArray =self.dataArr;
    [self.cityTableView setTableHeaderView:headerView];
    [self.cityTableView reloadData];
}

-(void)againLocationLabelClick{
    
    if (_againLocationClick) {
        _againLocationClick();
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

#pragma mark - SDCityPickerTableViewDelegate

-(void)tableView:(id)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SDCityInitial *cityInitial = self.dataArr[indexPath.section];
    
    SDCityModel *city = cityInitial.cityArrs[indexPath.row];
    DLog(@"cityName:%@",city.name);
    if (self.cityPickerBlock){
        self.cityPickerBlock(city);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)addNotification
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(cityDidSelected:) name:SDCityDidSelectedNotification object:nil];
}

-(void)cityDidSelected:(NSNotification *)notif{
    NSDictionary *userInfo = notif.userInfo;
    SDCityModel *city = userInfo[SDCityDidSelectKey];
    DLog(@"cityName:%@",city.name);
    
    if (self.cityPickerBlock){
        self.cityPickerBlock(city);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark TLSearchResultControllerDelegate
- (void) searchResultControllerDidSelectCity:(SDCityModel *)city
{
    if (self.cityPickerBlock){
        self.cityPickerBlock(city);
    }
    
    [self.searchResultVC dismissViewControllerAnimated:YES completion:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];

    
}

#pragma mark - Getter
- (UISearchController *) searchController
{
    if (_searchController == nil) {
        _searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultVC];
        [_searchController.searchBar setPlaceholder:@"请输入城市中文名称或拼音"];
        [_searchController.searchBar setBarTintColor:[UIColor colorWithWhite:0.95 alpha:1.0]];
        [_searchController.searchBar sizeToFit];
        [_searchController setSearchResultsUpdater:self.searchResultVC];
        [_searchController.searchBar.layer setBorderWidth:0.5f];
        [_searchController.searchBar.layer setBorderColor:[UIColor colorWithWhite:0.7 alpha:1.0].CGColor];
        _searchController.hidesNavigationBarDuringPresentation = NO;
        
    }
    return _searchController;
}

- (TLCityPickerSearchResultController *) searchResultVC
{
    if (_searchResultVC == nil) {
        _searchResultVC = [[TLCityPickerSearchResultController alloc] init];
        _searchResultVC.cityData = self.dataArr;
        _searchResultVC.searchResultDelegate = self;
        
    }
    return _searchResultVC;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
